#include "programexecute.h"
#include <helpers/connector.h>


class ProgramExecutePrivate{
	public:
	ProgramExecutePrivate():
			host("127.0.0.1")
			,port(51999)
		{}
		~ProgramExecutePrivate() {}
		Opl::Helpers::Connector connector;
		QString host;
		qint16 port;
};

ProgramExecute::ProgramExecute(QObject *parent) :
    QObject(parent), O_PRIVATE_CREATE(ProgramExecute)
{
	O_D(ProgramExecute);
	//d->
	connect(&d->connector, SIGNAL(result(QString)), this, SIGNAL(result(QString)));
}

ProgramExecute::~ProgramExecute()
{
	close();
	O_PRIVATE_DELETE;
}

void ProgramExecute::setup(const QString& host, qint16 port/* = 0*/)
{
	O_D(ProgramExecute);
	d->host = host;
	if (port > 0){
		d->port = port;
	}
}

void ProgramExecute::connectHost()
{
	O_D(ProgramExecute);
	if (!d->connector.isConnect()){
		d->connector.connectOptra(d->host, d->port);
	}
}

void ProgramExecute::close()
{
	O_D(ProgramExecute);
	if (d->connector.isConnect()){
		d->connector.close();
	}
}

void ProgramExecute::reconnect()
{
	close();
	connectHost();
}

void ProgramExecute::execute(const QString& program)
{
	O_D(ProgramExecute);
	connectHost();
	d->connector.send(program);
}
